package com.stylefeng.guns.rest.modular.catalogy.controller;

import com.stylefeng.guns.rest.common.CommonResponse;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.BussinessException;
import com.stylefeng.guns.rest.modular.artitle.controller.dto.ArticleDetailResponse;
import com.stylefeng.guns.rest.modular.artitle.service.ArticleService;
import com.stylefeng.guns.rest.modular.attent.controller.dto.AttentHotRequest;
import com.stylefeng.guns.rest.modular.attent.controller.dto.AttentUserResponse;
import com.stylefeng.guns.rest.modular.attent.service.AttentService;
import com.stylefeng.guns.rest.modular.attent.service.AttentUserService;
import com.stylefeng.guns.rest.modular.attent.service.CareService;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.*;
import com.stylefeng.guns.rest.modular.catalogy.service.CatalogyService;
import com.stylefeng.guns.rest.modular.content.service.ContentService;
import com.stylefeng.guns.rest.modular.viewrecord.service.ViewRecordService;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ASUS-PC on 2018/6/27.
 */
@RestController
@RequestMapping("/catalogy")
public class CatalogyController {

    @Autowired
    CatalogyService catalogyService;
    @Autowired
    ArticleService articleService;
    @Autowired
    AttentService attentService;
    @Autowired
    ViewRecordService viewRecordService;
    @Autowired
    private CareService careService;
    @Autowired
    private ContentService contentService;
    @Autowired
    private AttentUserService attentUserService;

    //获取分类列表
    @RequestMapping(value = "/catalogyList", method = RequestMethod.POST)
    public ResponseEntity<?> queryCatalogyList() {
        return ResponseEntity.ok(CommonResponse.ok(catalogyService.queryCatalogyList()));
    }


    //获取最新的文章列表
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResponseEntity<?> getCatalogyList(@RequestBody CatalogyListRequest request) {
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        List<ArticleListResponse> responses = catalogyService.queryArticleList(request);
        if(responses!=null && responses.size()>0){
            for (ArticleListResponse r:responses) {
                //是否已关注
                if(request.getUserId()>0){
                    int isAttent = attentService.queryAttntByUser(request.getUserId(),r.getUserId());
                    if(isAttent==1){
                        r.setIsAttent(1);
                    }
                }
                //查询所有的评论数
                int contentNums = contentService.queryVideoContentNums(r.getId());
                r.setContentNum(contentNums);
                //查询所有的点赞数
                int careNums = careService.queryAllCareByUser(r.getId());
                r.setCareNim(careNums);
            }
        }
        return ResponseEntity.ok(CommonResponse.ok(responses));
    }

    //获取最新的某类文章列表
    @RequestMapping(value = "/listCatalogy", method = RequestMethod.POST)
    public ResponseEntity<?> getCatalogyTypeList(@RequestBody CatalogyListRequest request) {
        if(request.getCatalogyId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        List<ArticleListResponse> responses = catalogyService.queryArticleTypeList(request);
        if(responses!=null && responses.size()>0) {
            for (ArticleListResponse r : responses) {
                //是否已关注
                if(request.getUserId()>0){
                    int isAttent = attentService.queryAttntByUser(request.getUserId(),r.getUserId());
                    if(isAttent==1){
                        r.setIsAttent(1);
                    }
                }
                //查询所有的评论数
                int contentNums = contentService.queryVideoContentNums(r.getId());
                r.setContentNum(contentNums);
                //查询所有的点赞数
                int careNums = careService.queryAllCareByUser(r.getId());
                r.setCareNim(careNums);
            }
        }
        return ResponseEntity.ok(CommonResponse.ok(responses));
    }

    //获取最新的某类文章列表(支持多分类)
    @RequestMapping(value = "/listCatalogy2", method = RequestMethod.POST)
    public ResponseEntity<?> getCatalogyTypeList2(@RequestBody CatalogyListRequest request) {
        if(request.getCatalogyId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        List<ArticleListResponse> responses = catalogyService.queryArticleTypeList2(request);
        if(responses!=null && responses.size()>0) {
            for (ArticleListResponse r : responses) {
                //是否已关注
                if(request.getUserId()>0){
                    int isAttent = attentService.queryAttntByUser(request.getUserId(),r.getUserId());
                    if(isAttent==1){
                        r.setIsAttent(1);
                    }
                }
                //查询所有的评论数
                int contentNums = contentService.queryVideoContentNums(r.getId());
                r.setContentNum(contentNums);
                //查询所有的点赞数
                int careNums = careService.queryAllCareByUser(r.getId());
                r.setCareNim(careNums);
            }
        }
        return ResponseEntity.ok(CommonResponse.ok(responses));
    }


    //获取热门排行文章
    @RequestMapping(value = "/listHot", method = RequestMethod.POST)
    public ResponseEntity<?> getCatalogyHotList(@RequestBody CatalogyHotRequest request) {
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        return ResponseEntity.ok(CommonResponse.ok(catalogyService.getCatalogyHotList(request)));
    }


    //文章详情
    @RequestMapping(value = "/articleDetail", method = RequestMethod.POST)
    public ResponseEntity<?> articleDetail(@RequestBody ArticleRequest request) {
        if(request.getArticleId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        //查询详情
        ArticleDetailResponse articleDetailResponse = articleService.getArticleDetail(request.getArticleId());
        if(articleDetailResponse==null){
            throw new BussinessException(BizExceptionEnum.CAN_NOT_FIND_ARTICLE);
        }
        //观看记录+1
        int num = articleDetailResponse.getViewNum()+1;
        articleService.addViewNum(articleDetailResponse.getId(),num);
        articleDetailResponse.setViewNum(num);
        if(request.getUserId()>0){
            //插入观看记录
            if(!viewRecordService.searchViewRecord(request)){
                viewRecordService.insetViewRecord(request.getUserId(),articleDetailResponse.getId());
            }
        }
        //是否已关注
        if(request.getUserId()>0){
            int isAttent = attentService.queryAttntByUser(request.getUserId(),articleDetailResponse.getUserId());
            if(isAttent==1){
                articleDetailResponse.setIsAttent(1);
            }
        }
        //是否点赞
        if(request.getUserId() >0){
            int isCare = careService.queryCareByUser(request.getUserId(),request.getArticleId());
            if(isCare == 1){
                articleDetailResponse.setIsCare(1);
            }
        }
        //查询所有的点赞数
        int careNums = careService.queryAllCareByUser(request.getArticleId());
        articleDetailResponse.setAllCareNums(careNums);
        //查询所有的评论数
        int contentNums = contentService.queryVideoContentNums(request.getArticleId());
        articleDetailResponse.setContentNum(contentNums);

        return ResponseEntity.ok(CommonResponse.ok(articleDetailResponse));

    }

    //获取客户关注的用户文章列表
    @RequestMapping(value = "/careList", method = RequestMethod.POST)
    public ResponseEntity<?> getUserCareDataList(@RequestBody AllListRequest request) {
        if(request.getUserId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(10);
        }
        //获取用户的关注用户列表
        AttentHotRequest r = new AttentHotRequest();
        r.setUserId(request.getUserId());
        r.setSize(999);
        r.setPage(1);
        List<AttentUserResponse> attentUsers = attentUserService.getAttentUserList(r);
        LinkedList<ArticleDetailResponse> allList = new LinkedList<>();
        if(attentUsers!=null && attentUsers.size()>0){
            for (int i = 0; i < attentUsers.size(); i++) {
                AttentHotRequest re = new AttentHotRequest();
                re.setUserId(attentUsers.get(i).getUserId());
                List<ArticleDetailResponse> lists = articleService.userAllList(re);
                if(lists!=null && lists.size()>0){
                    allList.addAll(lists);
                }
            }
            if(allList.size()==0){
                return ResponseEntity.ok(CommonResponse.ok(allList));
            }
            Collections.sort(allList, new Comparator<ArticleDetailResponse>() {
                @Override
                public int compare(ArticleDetailResponse o1, ArticleDetailResponse o2) {
                    //降序
                    return o2.getId() - o1.getId();
                }
            });

            for (ArticleDetailResponse d : allList) {
                //是否已关注
                if(request.getUserId()>0){
                    int isAttent = attentService.queryAttntByUser(request.getUserId(),d.getUserId());
                    if(isAttent==1){
                        d.setIsAttent(1);
                    }
                }
                //查询所有的评论数
                int contentNums = contentService.queryVideoContentNums(d.getId());
                d.setContentNum(contentNums);
                //查询所有的点赞数
                int careNums = careService.queryAllCareByUser(d.getId());
                d.setCareNim(careNums);
            }

            if(request.getCurrentSize()>=allList.size()){
                return ResponseEntity.ok(CommonResponse.ok(null));
            }
            if((request.getCurrentSize()+request.getSize())>allList.size()){
                return ResponseEntity.ok(CommonResponse.ok(allList.subList(request.getCurrentSize(),allList.size())));
            }else {
                if(allList.size()>=request.getSize()){
                    return ResponseEntity.ok(CommonResponse.ok(allList.subList(request.getCurrentSize(),(request.getCurrentSize()+request.getSize()))));
                }else {
                    return ResponseEntity.ok(CommonResponse.ok(allList.subList(request.getCurrentSize(),allList.size())));
                }
            }
        }else {
            return ResponseEntity.ok(CommonResponse.ok(allList));
        }
    }

    //文章详情
    @RequestMapping(value = "/catalogyDetail", method = RequestMethod.POST)
    public ResponseEntity<?> articleDetail(@RequestBody CatalogyNameRequest request) {
        if(TextUtils.isEmpty(request.getCatalogyName())){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        return ResponseEntity.ok(CommonResponse.ok(catalogyService.queryCatalogyByName(request.getCatalogyName())));
    }

}
